<template>
	<view class="contain">
		<view class="head"><image src="https://guangxins.oss-cn-shenzhen.aliyuncs.com/images/bg/buybg.jpg" mode="widthFix" style="width:100%;"></image></view>
		<view class="mt-9 flex justify-center"><text class="mid blod">绑定身份证</text></view>
		<view class="mt-5 flex justify-center flex-col m-auto w-[85%]">
			<view>
				<u-input placeholder="请输入查询身份证"
				 type="number"
				:customStyle="{'width':'550rpx'}" 
				:placeholderStyle="{'color':'#FFF'}" border="bottom" v-model="bindcode" @change="changeinput" fontSize="18" color="#000" maxlength="19"></u-input>
			</view>
			<view class="readxieyi">
				<view class="ra">
					 <u-checkbox shape="circle" :checked="checked" iconSize="18" size="25" @change="checkselected"></u-checkbox>
				</view>
				<view class="rb" @click="goxieyi">我已阅读并同意《服务协议》及《隐私政策》</view>
			</view>
			<view class="mt-3">
				<u-button type="primary" shape="circle" text="确认绑定" @click="bindCheck"></u-button>
			</view>
			<view class="mt-3">
				<u-button type="primary" shape="circle" text="跳过" :customStyle="dcustomstyle" @click="skip"></u-button>
			</view>
		</view>
	</view> <!-- contain -->
</template>

<script setup lang='ts'>
import { ref,reactive } from 'vue'
import {  onLoad,onShow } from '@dcloudio/uni-app'
import { userStore } from '@/stores/userStore'
const store = userStore();
let { hasLogin,userData } = userStore();

const dcustomstyle=reactive({
	"background":"#ccc",
	"border":"0px",
});


let checked=ref(false);

let bindcode=ref('');

const checkselected=()=>{
	checked.value=!checked.value;
	console.log("checked.value",checked.value);
}

const goxieyi=()=>{
	store.backRouter=store.xieyiLink;
	uni.$tools.GoUrl("/pages/webview/webview");
}

onShow(()=>{
	if(userData.binduseridcard!=null && userData.binduseridcard!=''){
		bindcode.value=userData.binduseridcard;
	}
});




const changeinput=(e:any)=>{
	console.log("输入发生变化:",e);
	userData.binduseridcard=e;
}



const bindCheck=()=>{
	
	if(userData.userinfo===null || hasLogin===false){
		//uni.$tools.Toast("会话已失效,请重新登录操作");
		uni.showModal({
			title:"温馨提示",
			content:"需要登录才能绑定，点击【确定登录】",
			showCancel:true,
			success:res=>{
				if(res.confirm){
					uni.$tools.GoUrl('/pages/login/login',0);
				}
			}
		})
		return;
	}
	
	if(!/\S/.test(bindcode.value)){
		uni.$tools.Toast("请输入查询身份证");
		return;
	}
	
	if(checked.value===false){
		uni.$tools.Toast("请勾选并同意服务协议");
		return;	
	}
	
	let paraData={
		id:userData.userinfo.id,
		bindcode: bindcode.value,
		sessionId:(uni.getStorageSync("sid")===null || uni.getStorageSync("sid")==="")?null:uni.getStorageSync("sid")
	}
	console.log("查询绑定参数:",paraData);
	uni.$tools.Reqeust('/api/bindCheck',paraData,'POST').then((res:any)=>{
			console.log('查询绑定结果:',res);
			if (res.data.code == 200) {
				if(res.data.targetUser!=null && res.data.targetUser[0]!=undefined){
					console.log("用户存在");
					store.targetUser=res.data.targetUser;
					uni.$tools.GoUrl("/pages/openPDF/openPDF",2);
				}
			}else{
				uni.$tools.Toast(res.data.msg);
			}
	})
}


const skip=()=>{
	uni.$tools.GoUrl("/pages/mine/mine",2);
}
</script>

<style>
	page{
		background:#f6eade;
	}
</style>
<style lang='scss' scoped>
	@import "binduser.scss";
</style>